package com.bhs.sansonglogistics.utils;

import android.content.Context;

import com.bhs.sansonglogistics.base.Constants;
import com.bhs.sansonglogistics.bean.PayParams;
import com.tencent.mm.opensdk.constants.Build;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class PayUtils {

    public static void weChatPay(Context context, PayParams payParams, IWXAPI msgApi) {
        msgApi = WXAPIFactory.createWXAPI(context, Constants.APP_ID, false);
        try {
            if (!msgApi.isWXAppInstalled()) {
                ToastUtil.show("请先安装微信");
                return;
            }
            //检查微信版本是否支持支付
            boolean isPaySupported = msgApi.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT;
            if (isPaySupported) {
                PayReq req = new PayReq();
                //微信开放平台审核通过的移动应用appid
                req.appId = Constants.APP_ID;
                //商户号
                req.partnerId = String.valueOf(payParams.getPartnerid());
                //预支付交易会话ID
                req.prepayId = payParams.getPrepayid();
                //随机字符串
                req.nonceStr = payParams.getNoncestr();
                //固定字段
                req.packageValue = "Sign=WXPay";
                //时间戳
                req.timeStamp = String.valueOf(payParams.getTimestamp());
                //签名
                req.sign = payParams.getSign();

                // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
                msgApi.sendReq(req);
            } else {
                ToastUtil.show("您当前微信不支持支付功能");
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }


}
